package com.ias.agdis_r.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ias.agdis_r.R;
import com.ias.agdis_r.manager.DataBaseManager;

public class ShowToast {

    private static ShowToast instance;
    private static final int SHOW_TOAST = 0;
    private static final int SHOW_LONG_TOAST = 1;
    private static final int SHOW_INT_TOAST = 2;
    private Context context;
    private Toast toast;
    private boolean bShowPttToast = false;
    private TextView toastText;
    private View convertView;

    public ShowToast() {
        this.context = DataBaseManager.getInstance().getContext();
    }

    public static ShowToast get() {
        if (instance == null) {
            instance = new ShowToast();
        }
        return instance;
    }

    public void setPttToastFlag(boolean bShowPttToast) {
        this.bShowPttToast = bShowPttToast;
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(android.os.Message msg) {

            if (toast == null) {
                toast = new Toast(context);
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(R.layout.toast, null);
                toastText = (TextView) convertView.findViewById(R.id.toastText);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
            }
            switch (msg.what) {
                case SHOW_TOAST: {
                    String message = (String) msg.obj;
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toastText.setText(message);
                    break;
                }
                case SHOW_LONG_TOAST: {
                    String message = (String) msg.obj;
                    toast.setDuration(Toast.LENGTH_LONG);
                    toastText.setText(message);
                    break;

                }
                case SHOW_INT_TOAST: {
                    int messageId = (Integer) msg.obj;
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toastText.setText(messageId);
                    break;
                }
            }
            toast.setView(convertView);
            toast.show();
        }
    };

    public void show(String msg) {
        Message message = Message.obtain();
        message.what = SHOW_TOAST;
        message.obj = msg;
        handler.sendMessage(message);
    }

    public void show(int msg) {
        Message message = Message.obtain();
        message.what = SHOW_INT_TOAST;
        message.obj = msg;
        handler.sendMessage(message);
    }

    public void showLongTime(String msg) {
        Message message = Message.obtain();
        message.what = SHOW_LONG_TOAST;
        message.obj = msg;
        handler.sendMessage(message);
    }

    public void showPtt(String msg) {

        if (bShowPttToast) {
            Message message = Message.obtain();
            message.what = SHOW_LONG_TOAST;
            message.obj = msg;
            handler.sendMessage(message);
        }
    }
}
